<?php

namespace App\Http\Controllers\Front;

use App\Models\AdminUsers;
use App\Models\AdvContent;
use App\Models\AgricGoods;
use App\Models\Article;
use App\Models\ArticleCategory;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class ArticleController extends Controller
{
    private $data = array();

    public function __construct(Request $request)
    {
        $this->data['pageTitle'] = '乡通';
    }


    /**
     * 技术
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function technology(Request $request)
    {
        $catrgoryId = 5;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['menuType'] = 'technology';
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 产业
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function industry(Request $request)
    {
        $catrgoryId = 4;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['is_region'] = 1;
        $this->data['menuType'] = 'industry';
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 普法
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function law(Request $request)
    {
        $catrgoryId = 10;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['menuType'] = 'law';
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 兴农
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function agriculture(Request $request)
    {
        $catrgoryId = 9;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['menuType'] = 'agriculture';
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 文化
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function culture(Request $request)
    {
        // 旅游观光广告位 3条
        $tourAdv = AdvContent::where('adv_type_id', 3)
            ->where('adv_type', 1)
            ->where('is_show', 1)
            ->with(['region'])
            ->limit(3)
            ->orderBy('sort', 'asc')
            ->orderBy('created_at', 'desc')
            ->get(['adv_name', 'adv_img_pc', 'adv_img', 'obj_id', 'region_id']);

        // 非遗文化广告位 3条
        $cultureAdv = AdvContent::where('adv_type_id', 2)
            ->where('adv_type', 1)
            ->where('is_show', 1)
            ->with(['region', 'article'])
            ->limit(3)
            ->orderBy('sort', 'asc')
            ->orderBy('created_at', 'desc')
            ->get(['adv_name', 'adv_img_pc', 'adv_img', 'obj_id', 'region_id']);

        $this->article_list_public_data();
        $this->data['is_region'] = 1; // 是否展示地区
        $this->data['tourAdv'] = $tourAdv;
        $this->data['cultureAdv'] = $cultureAdv;
        $this->data['menuType'] = 'culture';
        return view('front.article.culture', $this->data);
    }

    /**
     * 村务
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function village_notice(Request $request)
    {
        $catrgoryId = 7;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['is_region'] = 1; // 是否展示地区
        $this->data['positionInfo'] = ['name' => '村务', 'url' => ''];
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 党务
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function village_party(Request $request)
    {
        $catrgoryId = 3;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['is_region'] = 1; // 是否展示地区
        $this->data['positionInfo'] = ['name' => '党务', 'url' => ''];
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 政策
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function village_policy(Request $request)
    {
        $catrgoryId = 8;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['is_region'] = 1; // 是否展示地区
        $this->data['positionInfo'] = ['name' => '政策', 'url' => ''];
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 搜索
     * @param Request $request
     * @param String $keywords
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function search(Request $request, $keywords = '')
    {
        $catrgoryId = 0;

        $this->article_list_public_data($catrgoryId);
        $this->data['catrgory_id'] = $catrgoryId;
        $this->data['keywords'] = $keywords;
        $this->data['positionInfo'] = ['name' => '搜索结果', 'url' => ''];
        return view('front.article.category_article_lists', $this->data);
    }

    /**
     * 补全分类文章列表内容
     * @param int $catrgoryId 分类ID
     */
    private function article_list_public_data($catrgoryId = 0)
    {
        switch ($catrgoryId) {
            case 5:
                // 技术
                $banner_adv_id = 64; // banner广告位ID
                break;
            case 4:
                // 产业
                $banner_adv_id = 65;
                break;
            case 10:
                // 普法
                $banner_adv_id = 66;
                break;
            case 9:
                // 兴农
                $banner_adv_id = 67;
                break;

            default:
                $banner_adv_id = 0;
        }

        $banner_adv = AdvContent::getAdvContent(0, 1, 0, $banner_adv_id); // 获得banner广告位
        $catrgory = [];
        if ($catrgoryId) {
            $catrgory = ArticleCategory::where('parent_id', $catrgoryId)->where('is_show', 1)->orderBy('category_sort', 'asc')->get();
        }

        $this->data['banner_adv'] = !$banner_adv->isEmpty() ? $banner_adv[0] : $banner_adv;
        $this->data['article_catrgory_id'] = empty($catrgory) || $catrgory->isEmpty() ? $catrgoryId : $catrgory[0]->id; // 当前页面下展示的文章分类ID
        $this->data['catrgory'] = $catrgory;
        $this->data['is_region'] = 0; // 是否展示地区
        $this->data['hotGoods'] = AgricGoods::pc_hot_goods_list(); // 农品热销
    }

    /**
     * 发现
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function find(Request $request)
    {
// 发现 4条
        $video_article = Article::pc_article_list(['types' => 3], 8);

        $this->data['video_article'] = $video_article;

        $this->data['is_region'] = 1; // 是否展示地区
        $this->data['positionInfo'] = ['name' => '发现', 'url' => ''];
        return view('front.article.find', $this->data);
    }

    /**
     * ajax分类文章广告位
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function ajax_category_article_adv(Request $request)
    {
        $catrgoryId = $request->input('catrgory_id');
        $isRegion = $request->input('is_region');
        $number = $request->input('number') ?? 10; // 默认条数
        $catrgory = ArticleCategory::find($catrgoryId);
        $childCategoryId = ArticleCategory::getChild($catrgoryId);

        $advId = [0]; // 广告位ID
        if ($catrgory) {
            switch ($catrgoryId) {
                case 1:
                    // 市场

                    break;
                case 3:
                    // 党务

                    break;
                case 4:
                    // 产业

                    break;
                case 5:
                    // 技术
                    $advId[] = 1;
                    break;
                case 7:
                    // 村务

                    break;
                case 8:
                    // 政策
                    $advId[] = 5;
                    break;
                case 9:
                    // 兴农
                    $advId[] = 6; // 农品资讯
                    $advId[] = 19; // 农业科技
                    break;
                case 10:
                    // 普法
                    $advId[] = 7;
                    break;

                default:
            }

            $adv_content = AdvContent::where('adv_type', 1)->where('is_show', 1)->whereIn('adv_type_id', $advId)->orderBy('sort', 'asc')->take($number)->get();

            $advArticleQuery = Article::query();
            $advArticleQuery->with(['region_info', 'articles_author']);
            $advArticleQuery->where('is_show', 1);

            if (!$adv_content->isEmpty()) {
                $adv_content = $adv_content->toArray();
                $articleId = array_column($adv_content, 'obj_id');
                $advArticleQuery->whereIn('id', $articleId);
            } else {
                $advArticleQuery->whereIn('category_id', $childCategoryId);
                $advArticleQuery->orderBy('read_num', 'desc');
            }
            $advArticleQuery->take(3);
            $adv_list = $advArticleQuery->get();

            $articleQuery = Article::query();
            $articleQuery->with(['region_info', 'articles_author']);
            $articleQuery->whereIn('category_id', $childCategoryId);
            $articleQuery->where('is_show', 1);
            $articleQuery->orderBy('read_num', 'desc');
            $articleQuery->orderBy('id', 'desc');
            $articleQuery->take($number);
            $hot_list = $articleQuery->get();

            $this->data['adv_article'] = $adv_list;
            $this->data['article'] = $hot_list;
            $this->data['is_region'] = $isRegion;
            return view('front.article.ajax_category_article_adv', $this->data);
        }

    }

    /**
     * ajax分类文章列表
     * @param Request $request
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function ajax_category_article_lists(Request $request)
    {
        $where['keywords'] = $request->input('keywords');
        $where['author_id'] = $request->input('author_id');
        $where['is_admin'] = $request->input('is_admin') ?? 0;
        $where['categort_id'] = $request->input('category_id');
        $where['types'] = $request->input('types'); // 文章类型（1图文，2视频，3短视频）
        $pagesize = $request->pagesize ?? 20;

        $article_list = Article::pc_article_list($where, $pagesize);

        return ajaxSuccess('获得成功', $article_list);
    }

    /**
     * 文章详情页
     * @param Request $request
     * @param int $articleId
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function details(Request $request, $articleId = 0)
    {
        $article = Article::with(['article_category', 'articles_author', 'region_info'])->find($articleId);
        if (!$article) {
            return redirect()->back();
        }
        if ($article->video_url) {
            $article->video_url = img_url($article->video_url);
        }

        // 相关推荐
        $recommendArticle = Article::where('category_id', $article->category_id)->with(['article_category', 'articles_author', 'region_info'])->take(4)->get();

        // banner
        $banner_adv = AdvContent::getAdvContent(0, 1, 0, 75); // 获得banner广告位

        // 阅读数加一
        Article::where('id', $articleId)->increment('read_num', 1);

        $this->data['banner_adv'] = !$banner_adv->isEmpty() ? $banner_adv[0] : $banner_adv;
        $this->data['article'] = $article;
        $this->data['positionInfo'] = ['name' => $article->article_category->category_name, 'url' => ''];
        $this->data['recommendArticle'] = $recommendArticle;
        
        // 图文
        if ($article->types == 1) {
            if ($article->is_admin) {
                $this->data['authorArticleCount'] = Article::where('admin_id', $article->admin_id)->count();
            } else {
                $this->data['authorArticleCount'] = Article::where('user_id', $article->user_id)->count();
            }

            return view('front.article.details', $this->data);
        }

        // 短视频
        if ($article->types == 3) {
            return view('front.article.video_details', $this->data);
        }
        if($article->types ==4){
            
             return view('front.article.mp3_details', $this->data);
        }
    }

    /**
     * 作者详情页
     * @param Request $request
     * @param int $authorId
     * @param int $isAdmin
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\Foundation\Application|\Illuminate\View\View
     */
    public function author_index(Request $request, $authorId = 0, $isAdmin = 0)
    {
        $userInfo = [];
        if ($isAdmin == 1) {
            $adminUser = AdminUsers::find($authorId);
            $userInfo['id'] = $adminUser->id;
            $userInfo['name'] = $adminUser->name;
            $userInfo['avatar_url'] = $adminUser->avatar_url;
            $userInfo['fans_num'] = $adminUser->fans_num;
            $this->data['authorArticleCount'] = Article::where('admin_id', $authorId)->count();
        } else {
            $user = User::find($authorId);
            $userInfo['id'] = $user->id;
            $userInfo['name'] = $user->real_name ? $user->real_name : $user->user_name;
            $userInfo['avatar_url'] = $user->head_portrait_url;
            $userInfo['fans_num'] = $user->fans_num;
            $this->data['authorArticleCount'] = Article::where('user_id', $authorId)->count();
        }

        $this->data['userInfo'] = $userInfo;
        $this->data['authorId'] = $authorId;
        $this->data['isAdmin'] = $isAdmin;
        $this->data['positionInfo'] = ['name' => '作者主页', 'url' => ''];
        return view('front.article.author_index', $this->data);
    }
	//适老化帮助文档
	public function help(){
		  

      
        // banner
        $banner_adv = AdvContent::getAdvContent(0, 1, 0, 75); // 获得banner广告位
 
        $this->data['banner_adv'] = !$banner_adv->isEmpty() ? $banner_adv[0] : $banner_adv;
        
       

        
		return view('front.article.help',$this->data);
	}


}
